package com.sky.myapplication.kotlin.weight

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView

/**
 * Created by Min
 * Time 2022/10/15
 * Desc
 */
class MyItemDecoration : RecyclerView.ItemDecoration(){

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDraw(c, parent, state)
        val paint = Paint()
        paint.color = Color.RED
        val childCount = parent.childCount
        val layoutManager = parent.layoutManager
        for (i in 0..childCount-1){
            var child = parent.getChildAt(i)
            var width = layoutManager?.getDecoratedMeasuredWidth(child);
            var height = (i+1) * layoutManager?.getDecoratedMeasuredHeight(child)!!;
//            var left = layoutManager?.getLeftDecorationWidth(child)
//            var cx = (left!! / 2).toFloat();
//            var cy = (child.height/2 + child.top).toFloat()
//            c.drawCircle(cx, cy , 50f, paint)

            c.drawRect(0f, height!!.toFloat() - 50 , width!!.toFloat(), height!!.toFloat(), paint)
        }

//        var width : Float = parent.width.toFloat()
//        parent.getDecoratedBoundsWithMargins(child, mBounds)
//        c.drawRect(0f,0f, width,100f, paint)
    }

    override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDrawOver(c, parent, state)
//        var paint = Paint()
//        paint.color = Color.RED
//        c.drawRect(0f,100f,3000f,200f, paint)
    }

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        super.getItemOffsets(outRect, view, parent, state)
//        outRect.set(50, 10, 50, 10)
//        outRect.top=3
//        outRect.left=300
//        outRect.right=300
        outRect.bottom=50
    }
}